package org.gpf.game.tank.offline;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;

/**
 * 坦克类
 * @author gaopengfei
 * @date 2015-4-26 下午8:26:48
 */
public class Tank {

	public static final int TANK_WIDTH = 30;	// 坦克的宽高
	public static final int TANK_HEIGHT = 30;
	
	public static final int X_SPEED = 5;		// 两个方向上的分速度
	public static final int Y_SPEED = 5;
	
	private int life = 100;						// 生命值
	public void setLife(int life) {
		this.life = life;
	}
	public int getLife() {
		return life;
	}

	private BloodBar bb = new BloodBar();		// 血条

	private static int  ID_BASE = 0;
	private int id;
	
	private boolean bL,bU,bR,bD;				// 上下左右4个方向
	enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};	// 坦克移动的方向状态
	private Direction dir = Direction.STOP;		// 坦克的默认移动方向停止	
	private Direction ptDir = Direction.R;		// 炮筒方向默认向右
	
	private int step = r.nextInt(18) + 3;
	
	TankClient tc = null;						// 持有TankClient的引用
	
	private boolean good;						// 敌方还是我方
	public boolean isGood() {
		return good;
	}

	private boolean live = true;				// 坦克的存活状态
	public boolean isLive() {
		return live;
	}
	public void setLive(boolean live) {
		this.live = live;
	}
	
	private static Random r = new Random();		// 随机数产生器，一个类有一个就行了
	
	private int x;								// 坦克的位置
	private int y;
	
	private int oldX,oldY;						// 坦克没有撞墙之前的位置
	
	public Tank(int x, int y,boolean good) {
		this.id = ID_BASE++;
		this.x = x;
		this.y = y;
		this.good = good;
		this.oldX = x;
		this.oldY = y;
	}
	
	public Tank(int x,int y,boolean good,Direction dir,TankClient tc) {
		this(x, y, good);
		this.tc = tc;
		this.dir = dir;
	}
	
	/**
	 * 坦克自己把自己画出来
	 * @param g
	 */
	public void draw(Graphics g){
		
		if(!live){
			if (!good) {
				tc.tanks.remove(this);	// 如果是敌方的坦克去除
			}
			return;		// 如果坦克的为死亡状态就不需要画出坦克了
		}
		
		Color c = g.getColor(); // 保存默认的前景色
		// 根据坦克的阵营给坦克绘制不同的颜色（敌方和我们在界面中是不同的颜色）
		if (good){
			g.setColor(Color.RED);
			bb.draw(g);
		}
		else
			g.setColor(Color.BLUE);
		
		g.drawString("id:" + id, x, y - 15);
		g.fillOval(x, y, TANK_WIDTH, TANK_HEIGHT);
		g.setColor(c);			// 恢复原来画笔的颜色
		
		// 根据炮筒的方向画出直线
		switch (ptDir) {
		case L:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x, y + TANK_HEIGHT / 2);
			break;
		case LU:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x, y);
			break;
		case U:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_HEIGHT / 2, y);
			break;
		case RU:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH, y);
			break;
		case R:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH, y + TANK_HEIGHT / 2);
			break;
		case RD:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH, y + TANK_HEIGHT);
			break;
		case D:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x + TANK_WIDTH / 2, y + TANK_HEIGHT);
			break;
		case LD:
			g.drawLine(x + TANK_WIDTH / 2, y + TANK_HEIGHT / 2, x, y + TANK_HEIGHT);
			break;
		default:
			break;
		}
		
		move();	//	坦克移动
	}
	
	/**
	 * 根据当前的方向，移动到下一个位置
	 */
	private void move(){
		
		this.oldX = x; // 记录坦克上一次的位置
		this.oldY = y;
		
		switch (dir) {
		case L:
			x -= X_SPEED;
			break;
		case LU:
			x -= X_SPEED; // 这样写实际上是不精确的，它的速度是x和y方向的矢量和
			y -= Y_SPEED;
			break;
		case U:
			y -= Y_SPEED;
			break;
		case RU:
			x += X_SPEED;
			y -= Y_SPEED;
			break;
		case R:
			x += X_SPEED;
			break;
		case RD:
			x += X_SPEED;
			y += Y_SPEED;
			break;
		case D:
			y += Y_SPEED;
			break;
		case LD:
			x -= X_SPEED;
			y += Y_SPEED;
			break;
		case STOP:
			break;
		default:
			break;
		}
		
		/* 坦克移动的时候调整炮筒方向 */
		if (this.dir != Direction.STOP) {
			this.ptDir = this.dir;
		}
		
		// 让坦克不能出界
		if (x < 0) x = 0;
		if (y < 25) y = 25;	// 考虑到标题栏的问题
		if (x + TANK_WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - TANK_WIDTH;
		if (y + TANK_HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - TANK_HEIGHT;
		
		// 敌方坦克的运动是随机的
		if (!good) {
			// 使敌方坦克的运动更加逼真
			if (step==0) {
				Direction[] dirs = Direction.values();
				step = r.nextInt(15) + 8;
				int rn = r.nextInt(dirs.length);
				dir = dirs[rn];
			}
			step --;

			if(r.nextInt(40) > 30) fire();	// 利用随机数控制子弹的数量
		}
		
	}
	
	private void stay(){
		
		x = oldX;
		y = oldY;
	}
	
	/**
	 * 方向键按下的时候改变方向，按下Ctrl键开火
	 * @param e
	 */
	public void keyPressed(KeyEvent e){
		
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_LEFT:
			bL = true;
			break;
		case KeyEvent.VK_UP:
			bU = true;
			break;
		case KeyEvent.VK_RIGHT:
			bR = true;
			break;
		case KeyEvent.VK_DOWN:
			bD = true;
			break;
		case KeyEvent.VK_F8:
			if (!this.live) {
				this.setLive(true);
				this.setLife(100);
			}
			break;
		default:
			break;
		}
		locateDirection(); // 按下键的时候确定坦克的具体方向
	}
	
	/**
	 * 键盘抬起的时候停止坦克移动
	 * @param e
	 */
	public void keyReleased(KeyEvent e) {

		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_CONTROL:
			fire();
			break;
		case KeyEvent.VK_S:		// 绝招，向8个方向发出子弹
			superFire();
			break;
		case KeyEvent.VK_LEFT:
			bL = false;
			break;
		case KeyEvent.VK_UP:
			bU = false;
			break;
		case KeyEvent.VK_RIGHT:
			bR = false;
			break;
		case KeyEvent.VK_DOWN:
			bD = false;
			break;
		default:
			break;
		}
		locateDirection();
	}
	
	/**
	 * 设定坦克的具体方向
	 */
	private void locateDirection(){
		
		if (bL && !bU && !bR && !bD)
			dir = Direction.L;
		else if (bL && bU && !bR && !bD)
			dir = Direction.LU;
		else if (!bL && bU && !bR && !bD)
			dir = Direction.U;
		else if (!bL && bU && bR && !bD)
			dir = Direction.RU;
		else if (!bL && !bU && bR && !bD)
			dir = Direction.R;
		else if (!bL && !bU && bR && bD)
			dir = Direction.RD;
		else if (!bL && !bU && !bR && bD)
			dir = Direction.D;
		else if (bL && !bU && !bR && bD)
			dir = Direction.LD;
		else if (!bL && !bU && !bR && !bD)
			dir = Direction.STOP;
	}

	
	/**
	 * 坦克开火
	 * @return 子弹
	 */
	private Missile fire(){
		
		if(!live) return null;		// 如果坦克死了就不需要开火了	
		
		int x = this.x + TANK_WIDTH / 2 - Missile.MISSILE_WIDTH / 2;
		int y = this.y + TANK_HEIGHT / 2 - Missile.MISSILE_HEIGHT / 2;
		Missile m = new Missile(x, y, ptDir,good, tc); 							// 根据坦克的位置和方向产生一颗新的子弹
		tc.missiles.add(m);												// 将子弹加入到容器
		return m;
	}
	
	private Missile fire(Direction dir){
		
		if(!live) return null;		// 如果坦克死了就不需要开火了	
		
		int x = this.x + TANK_WIDTH / 2 - Missile.MISSILE_WIDTH / 2;
		int y = this.y + TANK_HEIGHT / 2 - Missile.MISSILE_HEIGHT / 2;
		Missile m = new Missile(x, y, dir,good, tc); 							// 根据坦克的位置和方向产生一颗新的子弹
		tc.missiles.add(m);												// 将子弹加入到容器
		return m;
	}
	
	/**
	 * 获取坦克的外切矩形
	 * @return
	 */
	public Rectangle getRect(){
		
		return new Rectangle(x, y, TANK_WIDTH, TANK_HEIGHT);
	}
	
	/**
	 * 撞墙
	 * @return
	 */
	public  boolean collidesWithWall(Wall w) {
		
		if(this.live&&this.getRect().intersects(w.getRect())){
			this.stay();
			return true;
		}
		return false;
	}
	
	/**
	 * 坦克和坦克撞击
	 * @param t
	 * @return
	 */
	public  boolean collidesWithTanks(List<Tank> tanks){
		
		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			if (this != t) {
				if (this.live && t.isLive() && this.getRect().intersects(t.getRect())) {
					this.stay();
					t.stay();
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 超级炸弹-按下此键向8个方向各发出一发炮弹
	 */
	private void superFire(){
		
		for(int i = 0;i < 8;i++){
			Direction[] dirs = Direction.values();
			fire(dirs[i]);
		}
		
	}
	
	/**
	 * 坦克血条
	 * @author gaopengfei
	 * @date 2015-5-8 下午12:36:12
	 */
	private class BloodBar{
		
		public void draw(Graphics g){
			Color c = g.getColor();
			g.setColor(Color.RED);
			g.drawRect(x, y - 10, TANK_WIDTH, 8);
			int w = TANK_WIDTH * life / 100;
			g.fillRect(x, y - 10, w, 8);
			g.setColor(c);
		}
	}
	
	/**
	 * 吃血
	 * @param b
	 * @return
	 */
	public boolean eatBlood(Blood b){
		
		if(this.live && b.isLive() && this.getRect().intersects(b.getRect())){
			this.setLife(100);	// 加满
			b.setLive(false);
			return true;
		}
		return false;
	}
	
}
